import {loadOneStepApi} from '@/api/gameTool/productionLine.api'
import Fraction from 'fraction.js'

export class GProductLineService {

	async loadOneStep(request: LoadOneStepRequest): Promise<LoadOneStepVo[]>

	async loadOneStep(request: LoadOneStepRequest2): Promise<LoadOneStepVo[]>

	async loadOneStep(request: LoadOneStepRequest | LoadOneStepRequest2): Promise<LoadOneStepVo[]> {
		if (this.isLoadOneStepRequest(request)) {
			return (await loadOneStepApi(request)).data
		} else if (this.isLoadOneStepRequest2(request)) {
			const fraction = new Fraction(request.quantityPerMinute)
			const loadOneStepRequest: LoadOneStepRequest = {
				itemId: request.itemId,
				quantityPerMinuteExactFraction: {
					numerator: fraction.n,
					denominator: fraction.d
				}
			}
			return (await loadOneStepApi(loadOneStepRequest)).data
		} else {
			throw Error('Invalid request: ' + JSON.stringify(request))
		}
	}

	private isLoadOneStepRequest(request: any): request is LoadOneStepRequest {
		return 'quantityPerMinuteExactFraction' in request
	}

	private isLoadOneStepRequest2(request: any): request is LoadOneStepRequest2 {
		return 'quantityPerMinute' in request
	}
}
